<template>
  <div>
    <el-form :model="form" inline ref="form">
      <el-form-item
        v-for="item in options"
        :key="item.prop"
        :label="item.label"
        :class="item.className"
      >
        <el-input
          v-if="item.type === 'input'"
          v-model="form[item.prop]"
          :placeholder="item.placeholder"
        ></el-input>
        <el-select
          v-else-if="item.type === 'select'"
          v-model="form[item.prop]"
          :placeholder="item.placeholder"
        >
          <el-option
            v-for="option in item.options"
            :key="option.value"
            :label="option.label"
            :value="option.value"
          >
          </el-option>
        </el-select>
        <template v-else-if="item.type === 'slider'">
          <span>{{ form[item.prop] ? form[item.prop][0] : 0 }}</span>
          <el-slider
            style="width: 200px; display: inline-block"
            v-model="form[item.prop]"
            range
            :max="item.max"
            :min="item.min"
          >
          </el-slider>
          <span>{{ form[item.prop] ? form[item.prop][1] : 100 }}</span>
        </template>

        <el-date-picker
          v-else-if="item.type === 'date'"
          v-model="form[item.prop]"
          type="daterange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" plain @click="search">搜索</el-button>
        <el-button plain @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  props: ["options"],
  name: "search",
  data() {
    return {
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近半年",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 180);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      value2: "",
      form: {},
    };
  },
  // 计算属性
  computed: {},
  methods: {
    reset() {
      // resetFields
      this.$refs["form"].resetFields();
    },
    search() {
      this.$emit("search", this.form);
    },
  },
  created() {},
  mounted() {},
};
</script>

<style scoped>
/* @import url(''); 引入css类 */
</style>